<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/powermockit%E6%B5%8B%E8%AF%95%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">378 字 ~2分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h2 id="1-概述"><strong>1. 概述</strong></h2>

<p>长期以来，借助 Mocking框架进行单元测试一直被认为是一种有用的实践，尤其是Mockito框架在近年来占据了主导地位。</p>

<p>并且为了促进体面的代码设计并使公共 API 变得简单，一些需要的特性被有意地遗漏了。然而，在某些情况下，这些缺点迫使测试人员编写繁琐的代码只是为了使模拟的创建可行。</p>

<p>这就是PowerMock框架发挥作用的地方。</p>

<p>PowerMockito是 PowerMock 的扩展 API，用于支持 Mockito。它以一种简单的方式提供了使用 Java 反射 API 的功能，以克服 Mockito 的问题，例如缺乏模拟final、静态或私有方法的能力。</p>

<p>本文将介绍 PowerMockito API 及其在测试中的应用。</p>

<h2 id="2-准备">2.准备</h2>

<p>为 Mockito 集成 PowerMock 支持的第一步是在 Maven POM 文件中包含以下两个依赖项：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-xml" data-lang="xml"><span style="color:#204a87;font-weight:bold">&lt;dependency</span><span style="color:#204a87;font-weight:bold">&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;groupId</span><span style="color:#204a87;font-weight:bold">&gt;</span>org.powermock<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;artifactId</span><span style="color:#204a87;font-weight:bold">&gt;</span>powermock-module-junit4<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;version</span><span style="color:#204a87;font-weight:bold">&gt;</span>1.6.4<span style="color:#204a87;font-weight:bold">&lt;/version&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;scope</span><span style="color:#204a87;font-weight:bold">&gt;</span>test<span style="color:#204a87;font-weight:bold">&lt;/scope&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;/dependency&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;dependency</span><span style="color:#204a87;font-weight:bold">&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;groupId</span><span style="color:#204a87;font-weight:bold">&gt;</span>org.powermock<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;artifactId</span><span style="color:#204a87;font-weight:bold">&gt;</span>powermock-api-mockito<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;version</span><span style="color:#204a87;font-weight:bold">&gt;</span>1.6.4<span style="color:#204a87;font-weight:bold">&lt;/version&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;scope</span><span style="color:#204a87;font-weight:bold">&gt;</span>test<span style="color:#204a87;font-weight:bold">&lt;/scope&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;/dependency&gt;</span>
复制代码</code></pre></div>
<p>接下来，我们需要通过应用以下两个注释来准备使用*PowerMockito*的测试用例：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#5c35cc;font-weight:bold">@RunWith</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">PowerMockRunner</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#5c35cc;font-weight:bold">@PrepareForTest</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">fullyQualifiedNames</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#34;com.baeldung.powermockito.introduction.*&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>*@PrepareForTest*注释中的*fullyQualifiedNames*元素表示我们想要模拟的类型的完全限定名称数组。在这种情况下，我们使用带有通配符的包名称来告诉*PowerMockito*准备*com.baeldung.powermockito.introduction*包中的所有类型以进行<strong>mock</strong>。</p>

<h2 id="3-模拟构造函数和最终方法"><strong>3. 模拟构造函数和最终方法</strong></h2>

<p>在本节中，我们将演示在使用*new*运算符实例化类时获取模拟实例而不是真实实例的方法，然后使用该对象模拟最终方法。协作类的构造函数和最终方法将被模拟，定义如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">CollaboratorWithFinalMethods</span> <span style="color:#ce5c00;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#000">String</span> <span style="color:#000">helloMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello World!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>首先，我们使用<em>PowerMockito</em> API创建一个模拟对象：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">CollaboratorWithFinalMethods</span> <span style="color:#000">mock</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">mock</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithFinalMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>接下来，设置一个期望值，告诉我们无论何时调用该类的无参数构造函数，都应该返回一个模拟实例而不是一个真实的实例：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">whenNew</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithFinalMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">withNoArguments</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thenReturn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">mock</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>让我们通过使用其默认构造函数实例化*CollaboratorWithFinalMethods*类来看看这个构造模拟是如何工作的，然后验证 PowerMock 的行为：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">CollaboratorWithFinalMethods collaborator = new CollaboratorWithFinalMethods();
verifyNew(CollaboratorWithFinalMethods.class).withNoArguments();
复制代码</code></pre></div>
<p>在下一步中，将期望设置为最终方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">when</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">collaborator</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">helloMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thenReturn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>然后执行此方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">String</span> <span style="color:#000">welcome</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">collaborator</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">helloMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>以下断言确认已在*协作者*对象上调用*helloMethod*方法，并返回*模拟*期望设置的值：**</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">Mockito</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">verify</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">collaborator</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">helloMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">assertEquals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">welcome</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>如果我们想模拟一个特定的 final 方法而不是对象内的所有 final 方法，<em>Mockito.spy(T object)</em> 方法可能会派上用场。第 5 节对此进行了说明。</p>

<h2 id="4-模拟静态方法"><strong>4.模拟静态方法</strong></h2>

<p>假设我们要模拟名为*CollaboratorWithStaticMethods*的类的静态方法 <em>。</em> 这个类声明如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">CollaboratorWithStaticMethods</span> <span style="color:#ce5c00;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">String</span> <span style="color:#000">firstMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">name</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello &#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">name</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34; !&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>

    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">String</span> <span style="color:#000">secondMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello no one!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>

    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">String</span> <span style="color:#000">thirdMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello no one again!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>为了模拟这些静态方法，我们需要使用<em>PowerMockito</em> API注册封闭类：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">mockStatic</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>或者，我们可以使用<em>Mockito.spy(Class class)</em> 方法来模拟特定的方法，如下一节所示。</p>

<p>接下来，可以设置期望值来定义调用时方法应返回的值：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">when</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">firstMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Mockito</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">anyString</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span>
  <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thenReturn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">when</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">secondMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thenReturn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Nothing special&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>或者调用*thirdMethod*方法时可能会设置抛出异常：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">doThrow</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">RuntimeException</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">when</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thirdMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>现在，执行前两个方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">String</span> <span style="color:#000">firstWelcome</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">firstMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Whoever&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">String</span> <span style="color:#000">secondWelcome</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">firstMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Whatever&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>上面的调用不是调用真实类的成员，而是委托给模拟的方法。以下断言证明模拟已经生效：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">assertEquals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">firstWelcome</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">assertEquals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">secondWelcome</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>我们还能够验证模拟方法的行为，包括调用方法的次数。在这种情况下，*firstMethod*已被调用两次，而*secondMethod*从未被调用：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">verifyStatic</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Mockito</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">times</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">2</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">firstMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Mockito</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">anyString</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
        
<span style="color:#000">verifyStatic</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Mockito</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">never</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">secondMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p><strong>注：</strong> 该*verifyStatic*必须调用方法正确之前，任何静态方法验证*PowerMockito*知道连续方法调用是需要验证的内容。</p>

<p>最后，静态的*thirdMethod*方法应该抛出一个<em>RuntimeException，</em> 就像之前在模拟上声明的那样。它由 *@Test*注释的*预期*元素验证：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#5c35cc;font-weight:bold">@Test</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">expected</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">RuntimeException</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">givenStaticMethods_whenUsingPowerMockito_thenCorrect</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">// other methods   
</span><span style="color:#8f5902;font-style:italic"></span>       
    <span style="color:#000">CollaboratorWithStaticMethods</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thirdMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#000">复制代码</span></code></pre></div>
<h2 id="5-部分模拟"><strong>5. 部分模拟</strong></h2>

<p><em>PowerMockito</em> API 允许使用*spy*方法模拟其中的一部分，而不是模拟整个类。以下类将用作协作者来说明 PowerMock 对部分模拟的支持：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">CollaboratorForPartialMocking</span> <span style="color:#ce5c00;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">String</span> <span style="color:#000">staticMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>

    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#000">String</span> <span style="color:#000">finalMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>

    <span style="color:#204a87;font-weight:bold">private</span> <span style="color:#000">String</span> <span style="color:#000">privateMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;Hello Baeldung!&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>

    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">String</span> <span style="color:#000">privateMethodCaller</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">privateMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34; Welcome to the Java world.&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
    <span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>让我们从*模拟*一个静态方法开始，它在上面的类定义中被命名为*staticMethod*。首先，使用<em>PowerMockito</em> API 部分模拟*CollaboratorForPartialMocking*类并为其静态方法设置期望：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">spy</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorForPartialMocking</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">when</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CollaboratorForPartialMocking</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">staticMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">thenReturn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;I am a static mock method.&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>然后执行静态方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">returnValue</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CollaboratorForPartialMocking</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">staticMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>模拟行为验证如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">verifyStatic</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">CollaboratorForPartialMocking</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">staticMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>以下断言通过将返回值与期望值进行比较来确认模拟方法实际上已被调用：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#000">assertEquals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;I am a static mock method.&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">returnValue</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>现在验证一下 final 和私有方法。为了说明这些方法的部分模拟，我们需要实例化该类并告诉<em>PowerMockito</em> API 来*监视*它：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">CollaboratorForPartialMocking collaborator = new CollaboratorForPartialMocking();
CollaboratorForPartialMocking mock = spy(collaborator);
复制代码</code></pre></div>
<p>上面创建的对象用于演示对 final 和私有方法的模拟。我们现在将通过设置期望并调用该方法来处理最终方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">when(mock.finalMethod()).thenReturn(&#34;I am a final mock method.&#34;);
returnValue = mock.finalMethod();
复制代码</code></pre></div>
<p>证明了部分模拟该方法的行为：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">Mockito.verify(mock).finalMethod();
复制代码</code></pre></div>
<p>测试验证调用*finalMethod*方法将返回与期望匹配的值：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">assertEquals(&#34;I am a final mock method.&#34;, returnValue);
复制代码</code></pre></div>
<p>类似的过程适用于私有方法。主要区别在于我们不能直接从测试用例中调用这个方法。基本上，私有方法将被来自同一类的其他方法调用。在*CollaboratorForPartialMocking*类中，*privateMethod*方法将由*privateMethodCaller*方法调用，我们将使用后者作为委托。让我们从期望和调用开始：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">when(mock, &#34;privateMethod&#34;).thenReturn(&#34;I am a private mock method.&#34;);
returnValue = mock.privateMethodCaller();
复制代码</code></pre></div>
<p>私有方法的mock得到确认：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">verifyPrivate(mock).invoke(&#34;privateMethod&#34;);
复制代码</code></pre></div>
<p>以下测试确保调用私有方法的返回值与预期相同：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">assertEquals(&#34;I am a private mock method. Welcome to the Java world.&#34;, returnValue);
复制代码</code></pre></div>
<h2 id="六-小结"><strong>六，小结</strong></h2>

<p>本文介绍了<em>PowerMockito</em> API，演示了它在解决开发人员在使用 Mockito 框架时遇到的一些问题中的用途。</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/powermockito%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E4%B8%80%E5%88%99/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/python%E6%89%A7%E8%A1%8Crequest%E8%AF%B7%E6%B1%82/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">169</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">12</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">151</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2022</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>